home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch20 / source / shrinkct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  2.0 KB  |  95 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4.  
  5.  
  6. #define INSIZE  512
  7. #define OUTSIZE 256
  8. #define BPV     2       /* Bytes per voxel */
  9.  
  10.  
  11. void main(int argc, char **argv)
  12. {
  13. int i,j,k,l,m;
  14. int Rfactor = INSIZE / OUTSIZE;         
  15. FILE *in, *out;
  16. short inframe[INSIZE][INSIZE];
  17. short outframe[OUTSIZE][OUTSIZE];
  18. int filesize, numslices;
  19. int temp;
  20.  
  21.  
  22.     /* Ensure proper syntax */
  23.     
  24.     if(argc != 3)
  25.       {
  26.       printf("\nUsage:: shrinkCT infile outfile\n\n");
  27.       exit(1);
  28.       }
  29.  
  30.  
  31.     /* Open the files */
  32.     
  33.     in = fopen(argv[1], "rb");
  34.     if(in == NULL)
  35.       {
  36.       printf("\nCould not open file %s.\n\n", argv[1]);
  37.       exit(2);
  38.       }
  39.  
  40.     out = fopen(argv[2], "wb");
  41.     if(out == NULL)
  42.       {
  43.       printf("\nCould not create file %s.\n\n", argv[2]);
  44.       exit(3);
  45.       }
  46.     
  47.     
  48.     /* Determine number of slices in the volume */
  49.     
  50.     filesize = filelength( fileno(in) );
  51.     numslices = filesize / INSIZE / INSIZE / BPV;
  52.     if(numslices * INSIZE * INSIZE * BPV != filesize)
  53.       {
  54.       printf("\nThe size of the input file is incorrect.\n\n");
  55.       exit(4);
  56.       }
  57.     
  58.     
  59.     /* Processing Loop */
  60.  
  61.     for(i=0; i<numslices; i++)
  62.       {
  63.       fread(inframe, INSIZE * INSIZE, BPV, in);
  64.       printf("Processing slice %d/%d.\n", i+1, numslices);
  65.  
  66.       /* Correct for negative Hounsfield values */
  67.       /* and mask out upper nibble */
  68.       
  69.       for(j=0; j<INSIZE; j++)
  70.         for(k=0; k<INSIZE; k++)
  71.           {
  72.           inframe[j][k] += 1024;
  73.           inframe[j][k] &= 0x0FFF;
  74.           }
  75.  
  76.       /* Size reduction by averaging */
  77.  
  78.       for(j=0; j<OUTSIZE; j++)
  79.         for(k=0; k<OUTSIZE; k++)
  80.           {
  81.           temp = 0;
  82.           for(l=0; l<Rfactor; l++)
  83.             for(m=0; m<Rfactor; m++)  
  84.               temp += inframe[j*Rfactor+l][k*Rfactor+m]; 
  85.           outframe[j][k] = temp / Rfactor / Rfactor;
  86.           }
  87.  
  88.       fwrite(outframe, OUTSIZE * OUTSIZE, BPV, out);
  89.       }
  90.  
  91.     fclose(in);
  92.     fclose(out);
  93.  
  94. }
  95.